package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import model.Hospede;
import model.Reserva;

public class ReservaDao {

	GenericDao gDao = new GenericDao();
	Connection con = GenericDao.getConnection();

	public boolean insereReserva(Reserva reserva){
		boolean inserido = false;
		try{
			PreparedStatement ps = con.prepareStatement("insert into t_reserva values (?,?,?,?)");
			ps.setString(1, reserva.getHospede().toString());
			ps.setInt(2, reserva.getNumeroPessoas());
			ps.setDate(3, (java.sql.Date) reserva.getDateInicial());
			ps.setDate(4, (java.sql.Date) reserva.getDataFinal());

			ps.execute();
			ps.close();
			inserido = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return inserido;
	}

	public Reserva consultaReserva(Hospede hospede){
		Reserva reserva = new Reserva();
		ResultSet rs = null;
		try{
			PreparedStatement ps = con.prepareStatement("select * from t_reserva where pk_cd_reserva = ?");
			ps.setString(1, reserva.getHospede().toString());
			
			ps.execute();
            ps.close();
			rs = ps.getResultSet();
			if (rs.next()){
				reserva.setDateInicial(rs.getDate(2));
				reserva.setDataFinal(rs.getDate(3));
			}
		} catch (Exception e){
			e.printStackTrace();
		}
		return reserva;
	}


	public boolean atualizaReserva(Reserva reserva){
		boolean editado = false;
		try{
			PreparedStatement ps = con.prepareStatement("update t_reserva set fk_cd_hospede = ?," +
					"fk_cd_quarto = ?, nr_pessoas = ?, dt_inicio = ?, dt_fim = ?,  where pk_cd_reserva = ?");

			ps.setString(1, reserva.getHospede().toString());
			ps.setInt(2, reserva.getNumeroPessoas());
			ps.setDate(2, (java.sql.Date) reserva.getDateInicial());
			ps.setDate(3, (java.sql.Date) reserva.getDataFinal());

			ps.execute();
			ps.close();
			editado = true;
		} catch(Exception e){
			e.printStackTrace();
		}
		return editado;
	}

	public boolean deletaReserva(Reserva reserva){
		boolean deletado = false;
		try{
			PreparedStatement ps = con.prepareStatement("delete t_reserva where fk_cd_reserva = ?");
			ps.setString(1, reserva.getHospede().toString());

			ps.execute();
			ps.close();
			deletado = true;
		} catch(Exception e){
			e.printStackTrace();
		}
		return deletado;
	}
}
